package com.ruoyi.project.repertory.controller;

import java.util.List;

import com.ruoyi.project.common.util.ChunQiuTool;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.repertory.domain.GoodsRepertory;
import com.ruoyi.project.repertory.service.IGoodsRepertoryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 库存Controller
 * 
 * @author lonsn
 * @date 2020-11-06
 */
@Controller
@RequestMapping("/goods/repertory")
public class GoodsRepertoryController extends BaseController
{
    private String prefix = "goods/repertory";

    @Autowired
    private IGoodsRepertoryService goodsRepertoryService;

    @RequiresPermissions("goods:repertory:view")
    @GetMapping()
    public String repertory()
    {
        return prefix + "/repertory";
    }

    /**
     * 查询库存列表
     */
    @RequiresPermissions("goods:repertory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsRepertory goodsRepertory)
    {
        //100表示总店，此时查询所有数据
        if(ChunQiuTool.convertNull(goodsRepertory.getDeptId()).equals("100")){
            goodsRepertory.setDeptId(null);
        }
        startPage();
        List<GoodsRepertory> list = goodsRepertoryService.selectGoodsRepertoryList(goodsRepertory);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @RequiresPermissions("goods:repertory:export")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsRepertory goodsRepertory)
    {
        //100表示总店，此时查询所有数据
        if(ChunQiuTool.convertNull(goodsRepertory.getDeptId()).equals("100")){
            goodsRepertory.setDeptId(null);
        }
        List<GoodsRepertory> list = goodsRepertoryService.selectGoodsRepertoryList(goodsRepertory);
        ExcelUtil<GoodsRepertory> util = new ExcelUtil<GoodsRepertory>(GoodsRepertory.class);
        return util.exportExcel(list, "repertory");
    }

    /**
     * 新增库存
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存
     */
    @RequiresPermissions("goods:repertory:add")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsRepertory goodsRepertory)
    {
        return toAjax(goodsRepertoryService.insertGoodsRepertory(goodsRepertory));
    }

    /**
     * 修改库存
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        GoodsRepertory goodsRepertory = goodsRepertoryService.selectGoodsRepertoryById(id);
        mmap.put("goodsRepertory", goodsRepertory);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存
     */
    @RequiresPermissions("goods:repertory:edit")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsRepertory goodsRepertory)
    {
        return toAjax(goodsRepertoryService.updateGoodsRepertory(goodsRepertory));
    }

    /**
     * 删除库存
     */
    @RequiresPermissions("goods:repertory:remove")
    @Log(title = "库存", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(goodsRepertoryService.deleteGoodsRepertoryByIds(ids));
    }

}
